package cn.retalsystem.service.impl;

import cn.retalsystem.bean.Buding;
import cn.retalsystem.bean.Chewei;
import cn.retalsystem.bean.Rental;
import cn.retalsystem.bean.Room;
import cn.retalsystem.dao.BudingMapper;
import cn.retalsystem.dao.CheweiMapper;
import cn.retalsystem.dao.RentalMapper;
import cn.retalsystem.dao.RoomMapper;
import cn.retalsystem.service.BudingService;
import cn.retalsystem.service.RoomService;
import cn.retalsystem.util.Cache;
import cn.retalsystem.util.CacheType;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * @Author: 官宝星
 * @Date: 2019/11/1 15:29
 * @Version 1.0
 * @描述：
 */
@Service
public class BudingServiceImpl implements BudingService {
    @Autowired
    private BudingMapper budingMapper;
    @Autowired
    RoomMapper roomMapper;
    @Autowired
    RentalMapper rentalMapper;
    @Autowired
    CheweiMapper cheweiMapper;
    @Autowired
    RoomService roomService;


    @Override
    @Cache(type = CacheType.SELECT)
    public List<Buding> selectAllBuding() {

        return budingMapper.selectAllBuding();
    }

    @Override
    @Cache(type = CacheType.SELECT)
    public Integer selectCount() {
        return budingMapper.selectCount();
    }

    @Override
    @Cache(type = CacheType.SELECT)
    public List<Buding> selectBudingByPage(Integer index, Integer size) {
        PageHelper.startPage(index,size);
        List<Buding> budings = budingMapper.selectAllBuding();
        return budings;
    }

    @Override
    @Cache(type = CacheType.UPDATE)
    public void addBuding(Buding buding) {
        budingMapper.insertSelective(buding);
    }

    @Override
    @Cache(type = CacheType.SELECT)
    public Buding selectBudingById(Integer id) {
        return budingMapper.selectByPrimaryKey(id);
    }

    @Override
    @Cache(type = CacheType.UPDATE)
    public void updateBuding(Buding buding) {
        budingMapper.updateByPrimaryKeySelective(buding);
    }


    @Override
    @Cache(type = CacheType.UPDATE)
    public void delBuding(Integer id) {
        //根据楼栋id查询房间信息
        List<Room> rooms = roomMapper.selectRoomByBudingId(id);
        for (Room room : rooms) {
            Integer rid=room.getId();
            //根据房间id查询租赁信息
            List<Rental> rentals = rentalMapper.selectRentalByRoomId(rid);
            for (Rental rental : rentals) {
                //删除租赁信息
                rentalMapper.deleteByPrimaryKey(rental.getId());
            }
            //删除房间信息
            roomMapper.deleteByPrimaryKey(rid);
        }
        //根据楼栋id删除车位信息
        List<Chewei> cheweis = cheweiMapper.selectCheweiByBudingId(id);
        for (Chewei chewei : cheweis) {
            cheweiMapper.deleteByPrimaryKey(chewei.getId());
        }
        //删除楼栋信息
        budingMapper.deleteByPrimaryKey(id);

    }


}
